Vue.component("pop-reason", {
  template:
    '<div class="alert alert-cancle-form show"><div class="over-close"></div><div class="box"><div class="title">{{title}}</div><div class="con"><textarea v-model="reason" name="" placeholder="请输入不合适原因（选填）" id="" cols="30" rows="10"></textarea></div><div class="operate"><a href="javascript:void(0)" class="btn btn-white" @click="cancelHandle">取消</a><a href="javascript:void(0)" class="btn" @click="sureHandle">确定</a></div></div></div>',
  props: ["id", "type"],
  data: function () {
    return {
      reason: "",
      isSend: false
    };
  },
  computed: {
    title: function () {
      return +this.type === 1 ? "确定要取消面试吗？" : "原因描述";
    }
  },
  methods: {
    sureHandle: function () {
      if (this.isSend) return;
      var status = +this.type === 1 ? 4 : 3;
      var url =
        +this.type === 1 ? "enterprise/interview" : "enterprise/user_recruit";
      var count = 0;
      var length = 1;
      this.isSend = true;

      if (this.id instanceof Array) {
        length = this.id.length;
        this.id.forEach(function (id) {
          this.sendRequest({
            id: id,
            status: status,
            url: url,
            success: successFn.bind(this),
            fail: failFn.bind(this)
          });
        }, this);
      } else {
        this.sendRequest({
          id: this.id,
          status: status,
          url: url,
          success: successFn.bind(this),
          fail: failFn.bind(this)
        });
      }
      function successFn(res) {
        count++;
        if (count < length) return;
        this.$message({
          type: "success",
          message: "操作成功"
        });
        this.isSend = false;
        this.$emit("sure", res);
      }
      function failFn(err) {
        count++;
        if (count < length) return;
        this.$message({
          type: "error",
          message: err.responseJSON ? err.responseJSON.message : "操作失败"
        });
        this.isSend = false;
      }
    },
    sendRequest: function (options) {
      var id = options.id;
      var params = {
        id: id,
        status: options.status || 3,
        reason: this.reason
      };
      var url = options.url || "enterprise/user_recruit";
      request(
        url + "/" + id,
        "PUT",
        JSON.stringify(params),
        function (res) {
          typeof options.success == "function" && options.success(res);
        },
        function (err) {
          typeof options.fail == "function" && options.fail(err);
        }
      );
    },
    cancelHandle: function () {
      this.$emit("cancel");
    }
  }
});

Vue.component("pop-interview", {
  template:
    '<div class="alert alert-input-form show interview-invite-box"><div class="over-close"></div><div class="box"><div class="title">填写面试信息 <a href="javascript:void(0)" class="btn-close" @click="cancelHandle"></a></div><ul class="form"><li><div class="tt">面试岗位</div><select v-model="recruitId" ref="recruitSelect" ><option v-for="(item, index) in recruitList" :key="index" :value="item.id">{{item.name}} {{item.salary_min}}~{{item.salary_max}}k</option></select></li><li><div class="tt">面试时间</div><el-date-picker v-model="formdata.time" value-format="timestamp" type="datetime" :picker-options="pickerOptions" style="width: 330px;"  placeholder="选择面试时间">  </el-date-picker></li><li><div class="tt">面试地点</div><input v-model.trim="formdata.address" type="text" placeholder="请填写" class="input"></li><li><div class="tt"><i style="text-align:left;width:33.33%;display:inline-block;">联</i><i style="text-align:center;width:33.33%;display:inline-block;">系</i><i style="text-align:right;width:33.33%;display:inline-block;">人</i></div><input v-model.trim="formdata.name" type="text" placeholder="请填写" class="input"></li><li><div class="tt">联系电话</div><input v-model.trim="formdata.tel" type="text" placeholder="请填写" class="input"></li><li><div class="tt"><i class="e2">备</i>注</div><input v-model="formdata.remark" type="text" placeholder="请填写" class="input"></li></ul><div class="operate"><a href="javascript:void(0)" class="btn" @click="invite">发起邀请</a></div></div></div>',
  props: ["id", "uid", "type"],
  data: function () {
    return {
      rules: {
        name: {
          test: function (val) {
            return val !== ''
          },
          msg: '联系人必须填写'
        },
        time: {
          test: function (val) {
            return val !== ''
          },
          msg: '面试时间必须选择'
        },
        tel: {
          test: function (val) {
            return val !== ''
          },
          msg: '联系电话必须填写'
        }
      },
      formdata: {
        time: "",
        name: "",
        tel: "",
        address: "",
        remark: ""
      },
      recruitId: "",
      recruitList: [],
      pickerOptions: {
        disabledDate: function (time) {
          return time.getTime() <= Date.now();
        }
      },
      isSend: false
    };
  },
  computed: {},
  watch: {
    'formdata.name': function (val) {
      if (val.length > 10) {
        this.formdata.name = val.substring(0, 10)
      }
    },
    'formdata.remark': function (val) {
      if (val.length > 30) {
        this.formdata.remark = val.substring(0, 30)
      }
    },
  },
  created: function () {
    this.getRecruitList();
  },
  mounted: function () {
    if (this.type == 1) {
      this.$refs.recruitSelect.setAttribute("disabled", true);
    } else {
      this.$refs.recruitSelect.removeAttribute("disabled");
    }
  },
  methods: {
    setData: function (data) {
      var formdata = this.formdata;
      Object.keys(formdata).forEach(function (key) {
        formdata[key] = data[key] || ''
      })
      this.recruitId = data.recruitId
    },
    getRecruitList: function () {
      var params = {
        page: 1,
        num: 999
      };
      request(
        "enterprise/recruit/list",
        "GET",
        params,
        successFn.bind(this),
        failFn.bind(this)
      );

      function successFn(res) {
        var data = res.data;
        this.recruitList = data.recruit;
      }

      function failFn(err) {
        console.log(err);
      }
    },
    invite: function () {
      if (this.isSend) return;
      var method = "POST";
      var params = this.formdata;
      var rules = this.rules;
      var url = "enterprise/interview";
      var count = 0;
      var failCount = 0;
      var successCount = 0;
      var length = 1;
      var isPass = Object.keys(rules).every(function (key) {
        var val = params[key];
        var rule = rules[key];
        var result = rule.test(val);
        if (!result) {
          this.$message({
            type: 'warning',
            message: rule.msg
          })
        }
        return result
      }, this);
      if (!isPass) return
      this.isSend = true;

      if (this.type === 1) {
        // 编辑面试信息
        method = "PUT";
        url += "/" + this.id;
        params.id = this.id;
        this.sendRequest({
          url: url,
          method: method,
          data: params,
          success: successFn.bind(this),
          fail: failFn.bind(this)
        });
      } else {
        params.id = this.recruitId;

        var ids = this.uid;
        if (ids instanceof Array) {
          length = ids.length;
          ids.forEach(function (id) {
            params.uid = id;
            this.sendRequest({
              url: url,
              method: method,
              data: params,
              success: successFn.bind(this),
              fail: failFn.bind(this)
            });
          }, this);
        } else {
          params.uid = ids;
          this.sendRequest({
            url: url,
            method: method,
            data: params,
            success: successFn.bind(this),
            fail: failFn.bind(this)
          });
        }
      }

      function successFn(res) {
        count++;
        successCount++;
        if (count < length) return;
        this.$message({
          type: "success",
          message:'邀请成功，请在待接受中查看'
        });
        this.isSend = false;
        this.$emit("sure", res);
      }

      function failFn(err) {
        count++;
        failCount++;
        if (count < length) return;
        this.$message({
          type: "error",
          message:
            count > 1
              ? successCount + "个操作成功，" + failCount + "个操作失败"
              : err.responseJSON
                ? err.responseJSON.message
                : "操作失败"
        });
        this.isSend = false;
      }
    },

    // 发送请求
    sendRequest: function (options) {
      var method = options.method || "POST";
      var params = options.data;
      var url = options.url;
      params.time /= 1000;
      request(
        url,
        method,
        JSON.stringify(params),
        function (res) {
          typeof options.success == "function" && options.success(res);
        },
        function (err) {
          typeof options.fail == "function" && options.fail(err);
        }
      );
    },
    cancelHandle: function () {
      this.$emit("cancel");
    }
  }
});
